/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * EditarProducto.java
 *
 * Created on 19/05/2010, 11:18:00 PM
 */

package ui;

import acceso.Producto;
import gestion.GestionSpring;
import interfaz.InterfaceEjp;
import javax.swing.JOptionPane;
import manejoExcepciones.ExcepcionesInventario;

/**
 *
 * @author Adriano
 */
public class EditarProducto extends javax.swing.JFrame {

    Long Codigo;
    PaginaPrincipal miPaginaPrincipal;

    public void setMiPaginaPrincipal(PaginaPrincipal miPaginaPrincipal) {
        this.miPaginaPrincipal = miPaginaPrincipal;
        //Muestra la info del producto seleccionado para modificar
        MostrarProductoModificar();
        //Se bloquean los JTextField
        NombreE.setEnabled(false);
        ComboTipo.setEnabled(false);
        DescripcionE.setEnabled(false);
        CantidadE.setEnabled(false);
        PrecioE.setEnabled(false);
        PresentacionE.setEnabled(false);
    }
    

    public void setCodigo(Long Codigo) {
        this.Codigo = Codigo;
    }


    /** Creates new form EditarProducto */
    public EditarProducto() {
        initComponents();
           
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        NombreE = new javax.swing.JTextField();
        ComboTipo = new javax.swing.JComboBox();
        DescripcionE = new javax.swing.JTextField();
        CantidadE = new javax.swing.JTextField();
        PrecioE = new javax.swing.JTextField();
        PresentacionE = new javax.swing.JTextField();
        BotonOkModificar = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        NOMBRE = new javax.swing.JRadioButton();
        TIPO = new javax.swing.JRadioButton();
        DESCRIPCION = new javax.swing.JRadioButton();
        CANTIDAD = new javax.swing.JRadioButton();
        PRECIO = new javax.swing.JRadioButton();
        PRESENTACION = new javax.swing.JRadioButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Inversiones E.J.P");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Editar Producto"));
        jPanel1.setName("jPanel1"); // NOI18N

        NombreE.setName("NombreE"); // NOI18N

        ComboTipo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Papeleria", "Limpieza" }));
        ComboTipo.setName("ComboTipo"); // NOI18N

        DescripcionE.setName("DescripcionE"); // NOI18N

        CantidadE.setName("CantidadE"); // NOI18N

        PrecioE.setName("PrecioE"); // NOI18N

        PresentacionE.setName("PresentacionE"); // NOI18N

        BotonOkModificar.setText("OK");
        BotonOkModificar.setName("BotonOkModificar"); // NOI18N
        BotonOkModificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BotonOkModificarActionPerformed(evt);
            }
        });

        jButton2.setText("Cancelar");
        jButton2.setName("jButton2"); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        NOMBRE.setText("Nombre:");
        NOMBRE.setName("NOMBRE"); // NOI18N
        NOMBRE.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                NOMBREActionPerformed(evt);
            }
        });

        TIPO.setText("Tipo:");
        TIPO.setName("TIPO"); // NOI18N
        TIPO.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TIPOActionPerformed(evt);
            }
        });

        DESCRIPCION.setText("Descripcion:");
        DESCRIPCION.setName("DESCRIPCION"); // NOI18N
        DESCRIPCION.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DESCRIPCIONActionPerformed(evt);
            }
        });

        CANTIDAD.setText("Cantidad:");
        CANTIDAD.setName("CANTIDAD"); // NOI18N
        CANTIDAD.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CANTIDADActionPerformed(evt);
            }
        });

        PRECIO.setText("Precio:");
        PRECIO.setName("PRECIO"); // NOI18N
        PRECIO.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                PRECIOActionPerformed(evt);
            }
        });

        PRESENTACION.setText("Presentacion:");
        PRESENTACION.setName("PRESENTACION"); // NOI18N
        PRESENTACION.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                PRESENTACIONActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(TIPO)
                            .addComponent(NOMBRE)
                            .addComponent(DESCRIPCION)
                            .addComponent(CANTIDAD)
                            .addComponent(PRECIO)
                            .addComponent(PRESENTACION))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(PrecioE, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 187, Short.MAX_VALUE)
                            .addComponent(CantidadE, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 187, Short.MAX_VALUE)
                            .addComponent(DescripcionE, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 187, Short.MAX_VALUE)
                            .addComponent(NombreE, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 187, Short.MAX_VALUE)
                            .addComponent(ComboTipo, javax.swing.GroupLayout.Alignment.LEADING, 0, 187, Short.MAX_VALUE)
                            .addComponent(PresentacionE, javax.swing.GroupLayout.DEFAULT_SIZE, 187, Short.MAX_VALUE))
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(BotonOkModificar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton2)
                        .addGap(36, 36, 36))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(11, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(NombreE, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(NOMBRE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ComboTipo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(TIPO))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(DescripcionE, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(DESCRIPCION))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(CantidadE, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(CANTIDAD))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(PrecioE, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(PRECIO))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(PresentacionE, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(PRESENTACION))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(BotonOkModificar)
                    .addComponent(jButton2)))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-344)/2, (screenSize.height-381)/2, 344, 381);
    }// </editor-fold>//GEN-END:initComponents

    private void BotonOkModificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BotonOkModificarActionPerformed

       try{
       //Se declaran los atributos del Producto
       String NombreM = NombreE.getText();
       String DescripcionM = DescripcionE.getText();
       int CantidadM = Integer.parseInt(CantidadE.getText());
       float PrecioM = Float.parseFloat(PrecioE.getText());
       String PresentacionM = PresentacionE.getText();
       char TipoM = 0;

       if (ComboTipo.getSelectedIndex() == 0)
           TipoM = 'P';
       else if (ComboTipo.getSelectedIndex() == 1)
           TipoM = 'L';

 //Si el JRadioButton del Nombre es seleccionado se guarda la nueva info.
       if (NOMBRE.isSelected() == true)
        {
            NombreM = NombreE.getText();

        }
//Si el JRadioButton del Tipo es seleccionado se guarda la nueva info.
       if (TIPO.isSelected() == true)
        {
           int Seleccion =  ComboTipo.getSelectedIndex();
                TipoM = 'I';

                   if (Seleccion == 0)
                   {
                       TipoM = 'P';
                   }
                   else if (Seleccion == 1)
                   {
                       TipoM = 'L';
                   }
        }
//Si el JRadioButton del Descripcion es seleccionado se guarda la nueva info.
       if (DESCRIPCION.isSelected() == true)
        {
            DescripcionM = DescripcionE.getText();

        }
//Si el JRadioButton de cantidad es seleccionado se guarda la nueva info.
       if (CANTIDAD.isSelected() == true)
        {
            CantidadM = Integer.parseInt(CantidadE.getText());

        }
//Si el JRadioButton del Precio es seleccionado se guarda la nueva info.
       if (PRECIO.isSelected() == true)
        {
            PrecioM = Float.parseFloat(PrecioE.getText());

        }
//Si el JRadioButton de Presentacion es seleccionado se guarda la nueva info.
       if (PRESENTACION.isSelected() == true)
        {
            PresentacionM = PresentacionE.getText();

        }

       
           //Verificamos que precio o cantidad sean numericos.
           float Precio = Float.parseFloat(PrecioE.getText());
           int Cantidad = Integer.parseInt(CantidadE.getText());

           //Verificamos que precio o cantidad no sean negativos ni iguales a cero.
           manejoExcepciones.ExcepcionesInventario.CamposNoNegativos(Precio, Cantidad);

           //Se construye el Producto a actualizar.
           Producto miProductoM = new Producto ();
           miProductoM.setIdProducto(Codigo);
           miProductoM.setNombre(NombreM);
           miProductoM.setDescripcion(DescripcionM);
           miProductoM.setCantidad(CantidadM);
           miProductoM.setPrecio(PrecioM);
           miProductoM.setPresentacion(PresentacionM);
           miProductoM.setTipo(TipoM);

           //Se hace el update del producto a traves de la interface.
           InterfaceEjp miInterfaceEjp = GestionSpring.getSingleton().MetodoGestion();
           miInterfaceEjp.registrarProducto(miProductoM);
           //Mostramos mensaje de exito.
           JOptionPane.showMessageDialog(this,"Modificacion Exitosa");
           //Refrescamos el JTable TablaProducto
           miPaginaPrincipal.Refrescar();
           this.setVisible(false);

       }catch(ExcepcionesInventario ExI2 ){

           JOptionPane.showMessageDialog(this,ExI2.getMessage());

       }
       catch(NumberFormatException NFE2){

           JOptionPane.showMessageDialog(this,"Los campos cantidad y precio deben ser numericos");

       }
       



}//GEN-LAST:event_BotonOkModificarActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed

        this.setVisible(false);
    }//GEN-LAST:event_jButton2ActionPerformed

    private void NOMBREActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_NOMBREActionPerformed
       NombreE.setEnabled(true);
}//GEN-LAST:event_NOMBREActionPerformed

    private void TIPOActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TIPOActionPerformed
        ComboTipo.setEnabled(true);
}//GEN-LAST:event_TIPOActionPerformed

    private void DESCRIPCIONActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DESCRIPCIONActionPerformed
        DescripcionE.setEnabled(true);
}//GEN-LAST:event_DESCRIPCIONActionPerformed

    private void CANTIDADActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CANTIDADActionPerformed
        CantidadE.setEnabled(true);
}//GEN-LAST:event_CANTIDADActionPerformed

    private void PRECIOActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_PRECIOActionPerformed
        PrecioE.setEnabled(true);
}//GEN-LAST:event_PRECIOActionPerformed

    private void PRESENTACIONActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_PRESENTACIONActionPerformed
        PresentacionE.setEnabled(true);
}//GEN-LAST:event_PRESENTACIONActionPerformed


    /**
     * Metodo en el que se muestra la informacion del producto a editar.
     *
     */
    public void MostrarProductoModificar()
    {

        InterfaceEjp miInterfaceEjp = GestionSpring.getSingleton().MetodoGestion();
        Producto miProductoModificar = miInterfaceEjp.BuscarProducto(Codigo);

        NombreE.setText(miProductoModificar.getNombre());
        DescripcionE.setText(miProductoModificar.getDescripcion());
        PrecioE.setText(String.valueOf(miProductoModificar.getPrecio()));
        PresentacionE.setText(miProductoModificar.getPresentacion());
        CantidadE.setText(String.valueOf(miProductoModificar.getCantidad()));

        if (miProductoModificar.getTipo() == 'P'){

            ComboTipo.setSelectedIndex(0);

        }else if (miProductoModificar.getTipo() == 'L'){

            ComboTipo.setSelectedIndex(1);

        }


    }


    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new EditarProducto().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BotonOkModificar;
    private javax.swing.JRadioButton CANTIDAD;
    private javax.swing.JTextField CantidadE;
    private javax.swing.JComboBox ComboTipo;
    private javax.swing.JRadioButton DESCRIPCION;
    private javax.swing.JTextField DescripcionE;
    private javax.swing.JRadioButton NOMBRE;
    private javax.swing.JTextField NombreE;
    private javax.swing.JRadioButton PRECIO;
    private javax.swing.JRadioButton PRESENTACION;
    private javax.swing.JTextField PrecioE;
    private javax.swing.JTextField PresentacionE;
    private javax.swing.JRadioButton TIPO;
    private javax.swing.JButton jButton2;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables

}
